//
//  VIPCenterViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/10/21.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class VIPCenterViewController: NSViewController {
    
    var days:Int = 0
    
    override func loadView() {
        let view = NSView.init(frame: NSRect.init(origin: CGPoint.zero, size: CGSize.init(width: 240, height: 220)))
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(255).cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    convenience init(days:Int) {
        self.init()
        self.days = days
        self.setupUI()
        self.loadData()
    }
    
    func setupUI() {
        self.view.addSubview(remainingLab)
        remainingLab.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
        }
        
        self.view.addSubview(dayLab)
        dayLab.snp.makeConstraints { (make) in
            make.top.equalTo(50)
            make.centerX.equalToSuperview()
        }
        
        self.view.addSubview(goButtom)
        goButtom.snp.makeConstraints { (make) in
            make.top.equalTo(100)
            make.centerX.equalToSuperview()
            make.width.equalTo(160)
            make.height.equalTo(36)
        }
        
        self.view.addSubview(buttomView)
        buttomView.snp.makeConstraints { (make) in
            make.width.bottom.centerX.equalToSuperview()
            make.top.equalTo(goButtom.snp.bottom).offset(10)
        }
        
        self.buttomView.addSubview(phoneLab)
        phoneLab.snp.makeConstraints { (make) in
            make.width.equalTo(self.view.frame.width-40)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
    }
    
    func loadData() {
        self.dayLab.attributedStringValue = self.attribudayLab(text: "\(self.days)天", stringProtocol: "\(self.days)")
        self.remainingLab.isHidden = self.days <= 0
        self.dayLab.isHidden = self.days <= 0
        
        let goButtomtop = self.days > 0 ? 100:20
        self.goButtom.snp.updateConstraints { (make) in
            make.top.equalTo(goButtomtop)
        }
        
        let windowHeight = self.days > 0 ? 220:150
        self.view.frame.size = CGSize.init(width: self.view.frame.width, height: CGFloat(windowHeight))
        //self.phoneLab.attributedStringValue = self.attribuPhoneLab(text: "升级/续费专业号，请拨打电话 400-8844-365 ，联系客服", stringProtocol: "400-8844-365")
    }
    
    func attribudayLab(text:String,stringProtocol:String) -> NSAttributedString {
        let muattribString = NSMutableAttributedString.init(string: text)
        let dayrange = text.range(of: stringProtocol)
        let nsRange = text.nsRange(from: dayrange!)
        muattribString.addAttributes([NSAttributedString.Key.font:NSFont.systemFont(ofSize: 32)], range:nsRange)
        return muattribString
    }
    
    func attribuPhoneLab(text:String,stringProtocol:String) -> NSAttributedString {
        let muattribString = NSMutableAttributedString.init(string: text)
        let dayrange = text.range(of: stringProtocol)
        let nsRange = text.nsRange(from: dayrange!)
        muattribString.addAttributes([NSAttributedString.Key.foregroundColor:color_main140], range:nsRange)
        return muattribString
    }
    
    lazy var dayLab: NSLabel = {
        let lab = NSLabel.label(title: "15天", textColor: color_gray(0), font: NSFont.systemFont(ofSize: 14))
        lab.alignment = .center
        lab.sizeToFit()
        return lab
    }()
    
    lazy var goButtom: ZHButton = {
        let btn = ZHButton.init()
        btn.zh_title = "升级/续费"
        btn.zh_font = NSFont.systemFont(ofSize: 14)
        btn.zh_color = color_gray(255)
        btn.backgroundNormalColor = color_gray(120)
        btn.backgroundSelectedColor = color_gray(120)
        btn.backgroundHoverColor = color_gray(120)
        btn.backgroundHighlightColor = color_gray(120)
        btn.hasBorder = true
        btn.canSelected = false
        btn.zh_cornerRadius = 18
        return btn
    }()
    
    lazy var buttomView: NSView = {
        let view = NSView.init()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(247).cgColor
        return view
    }()
    
    lazy var remainingLab: NSLabel = {
        let lab = NSLabel.label(title: "专业号剩余", textColor: color_gray(180), font: NSFont.systemFont(ofSize: 12))
        lab.sizeToFit()
        lab.alignment = .center
        return lab
    }()
    
    lazy var phoneLab: NSLabel = {
        let lab = NSLabel.label(title: "升级/续费专业号，请拨打电话400-8844-365，联系客服", textColor: color_gray(160), font: NSFont.systemFont(ofSize: 12))
        lab.maximumNumberOfLines = 3
        lab.sizeToFit()
        lab.alignment = .center
        return lab
    }()
    
}
